package com.weavernorth.test;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;

import java.lang.reflect.Type;

/**
 * @Author: bruseewang
 * @Package: com.weavernorth.test
 * @date: 2021/9/27 16:40
 * @description: json序列化
 */
public class JsonUtils {

    /**
     * 初始化User对象
     * @return user
     */
    private static User initUser(){
        User user = new User();
        user.setId("1");
        user.setName("jison");
        user.setPassword("jison");
        return user;
    }

    public static void main(String[] args) throws Exception {
        // fastjson用法
        System.out.println("fastjson用法");
        fastjson();
        // jackson用法
        System.out.println("jackson用法");
        jackson();
        // gson用法
        System.out.println("gson用法");
        gson();
    }

    private static void fastjson(){
        // 将Java对象序列化为Json字符串
        String objectToJson = JSON.toJSONString(initUser());
        System.out.println("序列化："+objectToJson);
        // 将Json字符串反序列化为Java对象
        User user = JSON.parseObject(objectToJson, User.class);
        System.out.println("反序列化："+user);
    }

    private static void jackson() throws Exception{
        ObjectMapper objectMapper = new ObjectMapper();
        // 将Java对象序列化为Json字符串
        String objectToJson = objectMapper.writeValueAsString(initUser());
        System.out.println("序列化："+objectToJson);
        // 将Json字符串反序列化为Java对象
        User user = objectMapper.readValue(objectToJson, User.class);
        System.out.println("反序列化："+user);
    }

    private static void gson(){
        Gson gson = new Gson();
        // 将Java对象序列化为Json字符串
        String objectToJson = gson.toJson(initUser());
        System.out.println("序列化："+objectToJson);
        // 将Json字符串反序列化为Java对象
        User user = gson.fromJson(objectToJson, (Type) User.class);
        System.out.println("反序列化："+user.toString());
    }
}
